package com.gykfbxxh.imovie.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.app.utils.util.HelpUtils;
import com.app.utils.util.ToastUtils;
import com.baidu.mobstat.StatService;
import com.gykfbxxh.imovie.AppApplication;
import com.gykfbxxh.imovie.R;
import com.gykfbxxh.imovie.bean.news.ShareBean;
import com.gykfbxxh.imovie.listener.HandleListener;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import static android.view.View.GONE;

/**
 * 所有的公用的分享方法
 * Created by xuxinghai on 2016/7/28.
 */
public class AppShareUtils implements OnClickListener {

    //弹窗逻辑数据
    public Activity mActivity;
    private AlertDialog dialog;// 默认的分享弹窗
    private ShareBean mShareBean;//分享的数据
    private boolean mIsHideSetLayout;//是否隐藏设置界面
    public String aid = "0";// 转发的文章id
    public String shareType = "0";//转发的类型

    //弹窗控件
    private ImageView mShareCancel;//退出
    private View rl_setfontsize, ll_setviewshow;//显示设置窗体
    //	private SeekBar light_seekBar;//亮度调节
    private DoMoreClickListener mDoMoreClickListener;
    private HandleListener handleListener;//额外操作监听


    public AppShareUtils(Activity activity) {
        this.mActivity = activity;
    }

    private AppShareUtils() {
    }


    private static AppShareUtils mAppShareUtils;


    public static AppShareUtils getInstance() {
        if (mAppShareUtils == null) {
            mAppShareUtils = new AppShareUtils();
        }
        return mAppShareUtils;
    }


    /**
     * 返回一个分享布局
     *
     * @param activity
     * @return View 分享弹窗布局
     */
    private View getShareDialogView(Activity activity) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }
        // 得到分享布局并且赋予单机事件
        View defaultView = activity.getLayoutInflater().inflate(R.layout.dialog_more_set, null);
        //微信
        View imageView = defaultView.findViewById(R.id.imageView_wechat);
        imageView.setOnClickListener(this);
        //朋友圈
        imageView = defaultView.findViewById(R.id.imageView_wechatmoments);
        imageView.setOnClickListener(this);
        //QQ
        imageView = defaultView.findViewById(R.id.imageView_qq);
        imageView.setOnClickListener(this);
        //QQ空间
        imageView = defaultView.findViewById(R.id.imageView_qzone);
        imageView.setOnClickListener(this);
        //QQ微博
        imageView = defaultView.findViewById(R.id.imageView_weibo);
        imageView.setOnClickListener(this);

        //退出
        mShareCancel = (ImageView) defaultView.findViewById(R.id.share_cancel);
        mShareCancel.setOnClickListener(this);


        return defaultView;
    }


    /**
     * 展示分享弹窗
     *
     * @param shareBean
     */
    public void  showShareDialog(ShareBean shareBean) {
        if (this.mActivity != null && !this.mActivity.isFinishing()) {
            this.mShareBean = shareBean;
            if (dialog == null) {
                View view = getShareDialogView(mActivity);

                if (view == null) {
                    ToastUtils.showText("分享数据获取失败,请重试");
                    return;
                }
                dialog = new AlertDialog.Builder(mActivity).create();
                dialog.setCanceledOnTouchOutside(true);
                if (mActivity != null && !mActivity.isFinishing()) {
                    dialog.show();
                    Window window = dialog.getWindow();
                    window.setWindowAnimations(R.style.dialogWindowAnim);
                    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                            WindowManager.LayoutParams.WRAP_CONTENT);
                    window.setBackgroundDrawableResource(android.R.color.transparent); //设置对话框背景为透明
                    window.setGravity(Gravity.BOTTOM);
                    window.setContentView(view);
                }
//                try {
//                    //设置友盟分享dialog
//                    ProgressDialog dialog = new ProgressDialog(mActivity);
//                    dialog.setMessage("正在分享");
//                    Config.dialog = dialog;
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
                return;
            }
            this.dialog.show();
        }

    }



//	/**
//	 * 滑动屏幕亮度的监听
//	 */
//	private SeekBar.OnSeekBarChangeListener seekBarChange = new SeekBar.OnSeekBarChangeListener() {
//
//		int defalutValue;
//
//		@Override
//		public void onStopTrackingTouch(SeekBar seekBar) {
//			//保存亮度
//			UserConfigTools.screen_brightnes = defalutValue;
//			AppConfigManager.getInstance().writeConfig(ConfigKeyConstant.option_Screen_brightnes, defalutValue);
//		}
//
//		@Override
//		public void onStartTrackingTouch(SeekBar seekBar) {
//		}
//
//		@Override
//		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//			defalutValue = progress;
//			BrightnessUtils.setLocalScreenBright(mActivity, progress);
//		}
//	};


    @Override
    public void onClick(View v) {

        if (v == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.imageView_wechat://微信
                StatService.onEvent(mActivity, "031", "弹窗微信分享", 1);// 统计的点击的次数
                setWXShareContent(this.mActivity, this.mShareBean, null);
                break;
            case R.id.imageView_wechatmoments://朋友圈
                StatService.onEvent(mActivity, "031", "弹窗朋友圈分享", 1);// 统计的点击的次数
                setWXCShareContent(this.mActivity, this.mShareBean, null);
                break;
            case R.id.imageView_qq://qq
                StatService.onEvent(mActivity, "031", "弹窗qq分享", 1);// 统计的点击的次数
                setQQShareContent(this.mActivity, this.mShareBean, null);
                break;
            case R.id.imageView_qzone://qq空间
                StatService.onEvent(mActivity, "031", "弹窗qq空间分享", 1);// 统计的点击的次数
                setQZONEShareContent(this.mActivity, this.mShareBean);
                break;
            case R.id.imageView_weibo://微博
                StatService.onEvent(mActivity, "031", "弹窗微博分享", 1);// 统计的点击的次数
                setSinaShareContent(this.mActivity, this.mShareBean);
                break;
            case R.id.share_cancel://返回
                if (mIsHideSetLayout) {
                    ll_setviewshow.setVisibility(View.VISIBLE);
                    rl_setfontsize.setVisibility(GONE);
                    mIsHideSetLayout = false;
                } else {
                    cancleDialog();
                }

                break;

        }
    }



    /**
     * 复制
     */
    private void copy() {
        if (mShareBean == null) {
            ToastUtils.makeText(R.drawable.handle_success, "复制失败");
            return;
        }
        HelpUtils.copy(mShareBean.getShareUrl(), AppApplication.getInstance());
        cancleDialog();
        ToastUtils.makeText(R.drawable.handle_success, "已复制");
    }

    /**
     * 隐藏dialog
     */
    public void cancleDialog() {
        try {
            if (this.dialog != null && this.dialog.isShowing()) {
                this.dialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /******************* 第三方分享调用方法 start *******************/
//    /**
//     * 调用友盟自带的分享弹窗
//     *
//     * @param activity
//     * @param shareBean
//     */
//    public void UMShareView(Activity activity, ShareBean shareBean) {
//        if (shareBean == null) {
//            shareBean = new ShareBean();
//        }
//        aid = shareBean.getArticleId();
//        UMImage urlImage = new UMImage(activity, shareBean.getShareImg());
//        new ShareAction(activity).setDisplayList(SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
//                .withTitle(shareBean.getShareTitle(null))
//                .withText(shareBean.getShareSummary())
//                .withMedia(urlImage)
//                .setCallback(umShareListener)
//                .open();
//    }

    /**
     * 设置微信朋友圈分享的内容
     *
     * @param activity
     * @param newsContent
     */
    public void setWXCShareContent(Activity activity, ShareBean newsContent, UMShareListener shareListener) {
        if (newsContent == null) {
            newsContent = new ShareBean();
        }
        aid = newsContent.getArticleId();
        shareType = newsContent.getShareType();

        UMWeb web = new UMWeb(newsContent.getShareWxurl());
        UMImage urlImage = new UMImage(activity, newsContent.getShareImg());
        web.setTitle(newsContent.getShareTitle(null));//标题
        web.setThumb(urlImage);  //缩略图
        web.setDescription(newsContent.getShareSummary());//描述

        new ShareAction(activity)
                .withMedia(web)
                .share();

//        UMImage urlImage = new UMImage(activity, newsContent.getShareImg());
//        new ShareAction(activity).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).setCallback(shareListener == null ? umShareListener : shareListener)
//                .withTitle(newsContent.getShareTitle(null))
//                .withText(newsContent.getShareSummary())
//                .withTargetUrl(newsContent.getShareWxurl())
//                .withMedia(urlImage)
//                .share();
        cancleDialog();
    }

    /**
     * 设置微信分享的内容
     *
     * @param activity
     * @param newsContent
     */
    public void setWXShareContent(Activity activity, ShareBean newsContent, UMShareListener shareListener) {
        if (newsContent == null) {
            newsContent = new ShareBean();
        }
        aid = newsContent.getArticleId();
        shareType = newsContent.getShareType();

        UMWeb web = new UMWeb(newsContent.getShareUrl());
        UMImage urlImage = new UMImage(activity, newsContent.getShareImg());
        web.setTitle(newsContent.getShareTitle(null));//标题
        web.setThumb(urlImage);  //缩略图
        web.setDescription(newsContent.getShareSummary());//描述

        new ShareAction(activity)
                .withMedia(web)
                .share();

//        UMImage urlImage = new UMImage(activity, newsContent.getShareImg());
//        new ShareAction(activity).setPlatform(SHARE_MEDIA.WEIXIN).setCallback(shareListener == null ? umShareListener : shareListener)
//                .withMedia(urlImage)
//                .withText(newsContent.getShareSummary())
//                .withTitle(newsContent.getShareTitle(null))
//                .withTargetUrl(newsContent.getShareUrl())
//                .share();
        cancleDialog();
    }

    //设置QQ分享的内容
    public void setQQShareContent(Activity activity, ShareBean newsContent, UMShareListener shareListener) {
        if (newsContent == null) {
            newsContent = new ShareBean();
        }
        aid = newsContent.getArticleId();
        shareType = newsContent.getShareType();

        UMWeb web = new UMWeb(newsContent.getShareUrl());
        UMImage urlImage = new UMImage(activity, newsContent.getShareImg());
        web.setTitle(newsContent.getShareTitle(null));//标题
        web.setThumb(urlImage);  //缩略图
        web.setDescription(newsContent.getShareSummary());//描述

        new ShareAction(activity)
                .withMedia(web)
                .share();

//        UMImage urlImage = new UMImage(activity, newsContent.getShareImg());
//        new ShareAction(activity).setPlatform(SHARE_MEDIA.QQ).setCallback(shareListener == null ? umShareListener : shareListener)
//                .withText(newsContent.getShareSummary())
//                .withTargetUrl(newsContent.getShareUrl())
//                .withMedia(urlImage)
//                .withTitle(newsContent.getShareTitle(null))
//                .share();
        cancleDialog();
    }


    //设置QQ空间分享的内容
    public void setQZONEShareContent(Activity activity, ShareBean newsContent) {
        if (newsContent == null) {
            newsContent = new ShareBean();
        }
        aid = newsContent.getArticleId();
        shareType = newsContent.getShareType();

        UMWeb web = new UMWeb(newsContent.getShareUrl());
        UMImage urlImage = new UMImage(activity, newsContent.getShareImg());
        web.setTitle(newsContent.getShareTitle(null));//标题
        web.setThumb(urlImage);  //缩略图
        web.setDescription(newsContent.getShareSummary());//描述

        new ShareAction(activity)
                .withMedia(web)
                .share();

//        UMImage urlImage = new UMImage(activity, newsContent.getShareImg());
//        new ShareAction(activity).setPlatform(SHARE_MEDIA.QZONE).setCallback(umShareListener)
//                .withTitle(newsContent.getShareTitle(null))
//                .withText(newsContent.getShareSummary())
//                .withMedia(urlImage)
//                .withTargetUrl(newsContent.getShareUrl())
//                .share();
        cancleDialog();
    }

	//设置微博分享的内容
	public void setSinaShareContent(Activity activity, ShareBean newsContent){
		if(newsContent == null){
			newsContent = new ShareBean();
		}
		aid = newsContent.getArticleId();
		shareType = newsContent.getShareType();

        UMWeb web = new UMWeb(newsContent.getShareUrl());
        UMImage urlImage = new UMImage(activity, newsContent.getShareImg());
        web.setTitle(newsContent.getShareTitle(null));//标题
        web.setThumb(urlImage);  //缩略图
        web.setDescription(newsContent.getShareSummary());//描述

        new ShareAction(activity)
                .withMedia(web)
                .share();

//		UMImage urlImage = new UMImage(activity, newsContent.getShareImg());
//		new ShareAction(activity).setPlatform(SHARE_MEDIA.SINA).setCallback(umShareListener)
//				.withTitle(newsContent.getShareTitle(null))
//				.withText(newsContent.getShareSummary()+" @迷彩虎军事 ")
//				.withMedia(urlImage)
//				.withTargetUrl(newsContent.getShareUrl())
//				.share();
		cancleDialog();
	}





    //分享回调监听
    public UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            //统计分享的文章
            writeShareCount(platform);
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            ToastUtils.makeText(R.drawable.handle_fail, "分享失败");
            String typename ;
            if (platform != null) {
                if (SHARE_MEDIA.SINA.equals(platform.toSnsPlatform().mPlatform)) {
                    typename = "新浪微博";
                } else if (SHARE_MEDIA.WEIXIN.equals(platform.toSnsPlatform().mPlatform)) {
                    typename = "微信";
                } else if (SHARE_MEDIA.WEIXIN_CIRCLE.equals(platform.toSnsPlatform().mPlatform)) {
                    typename = "朋友圈";
                } else if (SHARE_MEDIA.QQ.equals(platform.toSnsPlatform().mPlatform)) {
                    typename = "qq";
                } else if (SHARE_MEDIA.QZONE.equals(platform.toSnsPlatform().mPlatform)) {
                    typename = "qq空间";
                } else {
                    typename = "无类型";
                }
            }else{
                typename = "没有获取到类型";
            }
            StatService.onEvent(mActivity, "035",  "分享失败", 1);// 统计的点击
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            ToastUtils.makeText("分享取消");
        }


    };

    /**
     * 返回分享dialog
     *
     * @return
     */
    public Dialog getShareDialog() {
        return dialog;
    }


    /**
     * 统计分享的文章
     */
    public void writeShareCount(SHARE_MEDIA platform) {

//        ParamMap mapParms = new ParamMap();
//        String type = "0";
//        String typename ;
//        if (platform != null) {
//            if (SHARE_MEDIA.SINA.equals(platform.toSnsPlatform().mPlatform)) {
//                type = "1";
//                typename = "新浪微博";
//            } else if (SHARE_MEDIA.WEIXIN.equals(platform.toSnsPlatform().mPlatform)) {
//                type = "2";
//                typename = "微信";
//            } else if (SHARE_MEDIA.WEIXIN_CIRCLE.equals(platform.toSnsPlatform().mPlatform)) {
//                type = "3";
//                typename = "朋友圈";
//            } else if (SHARE_MEDIA.QQ.equals(platform.toSnsPlatform().mPlatform)) {
//                type = "4";
//                typename = "qq";
//            } else if (SHARE_MEDIA.QZONE.equals(platform.toSnsPlatform().mPlatform)) {
//                type = "5";
//                typename = "qq空间";
//            } else {
//                type = "0";
//                typename = "无类型";
//            }
//        }else{
//            typename = "没有获取到类型";
//        }
//        if (UserInforManager.getInstance().isLogin()) {
//            mapParms.putEncode("uid", UserInforManager.getInstance().getUserInfor().getUid());
//        } else {
//            mapParms.putEncode("uid", "0");
//        }
//        mapParms.putEncode("shareType", type);
//        if(TextUtils.isEmpty(shareType)){
//            shareType = TypeConstant.SHARE_TYPE_NEWS;
//        }
//        mapParms.putEncode("operatorType", shareType);
//        mapParms.putEncode("articleId", aid);
//        HttpUtils.ParmsMapMD5(mapParms);
//        HttpListener<DataBean<GetTaskBean>> httpListener = new HttpListener<DataBean<GetTaskBean>>() {
//            @Override
//            public void onError(VolleyError error) {
//                ToastUtils.makeText(R.drawable.handle_success, "分享成功");
//            }
//
//            @Override
//            public void onSuccess(DataBean<GetTaskBean> response) {
//
//                if (response.noError() && response.getData() != null
//                        && response.getData().getIsGetTask("分享文章")) {
//                    //刷新任务
//                    UserInforManager.getInstance().getUserTask(mActivity, false);
//                }else{
//                    ToastUtils.makeText(R.drawable.handle_success, "分享成功");
//                }
//
//            }
//        };
//
//        //请求网络
//        HttpUtils.addPostRequest(URIContant.NEWS_SHARE_COUNT, new TypeToken<DataBean<GetTaskBean>>() {
//        }.getType(), null, mapParms, httpListener);


    }

    /******************* 第三方分享调用方法 end *******************/




    public interface DoMoreClickListener{
        void onDeleteClick();

        void onDefriendClick();

    }

    public void setOnDoMoreClickListener(DoMoreClickListener mDoMoreClickListener){
        this.mDoMoreClickListener = mDoMoreClickListener;
    }

    public void setHandleListener(HandleListener handleListener){
        this.handleListener = handleListener;
    }
}
